package com.itheima.health.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.model.dtos.AddSetMealDto;
import com.itheima.health.model.dtos.UpdateSetMealDto;
import com.itheima.health.model.pojos.QueryString;
import com.itheima.health.model.pojos.Setmeal;
import org.example.aliyun.AliyunOSSUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private CheckGroupMapper CheckGroupMapper;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    public ResultPageData getSetMealPage(PageParam pageParam) {
        PageHelper.startPage(pageParam.getCurrentPage(), pageParam.getPageSize());
        QueryString queryString1 = new QueryString();
        String queryString=null;
        if (pageParam.getQueryString()!=null &&pageParam.getQueryString()!=""){
            queryString = pageParam.getQueryString();
            if (queryString.matches("[0-9]+")) {
                queryString1.setCode(queryString);
            } else if (queryString.matches("[a-zA-Z]+")) {
                queryString1.setHelpCode(queryString);
            }
            else {
                queryString1.setName(queryString);
            }
        }
        List<Setmeal> setmealList= setMealMapper.getSetMealPage(queryString1);
        Page<Setmeal> setmealList1 = (Page<Setmeal>) setmealList;
        return new ResultPageData(setmealList1.getTotal(),setmealList1.getResult());
    }

    public String uploadImageById(MultipartFile file) throws Exception {
        // 拿到上传的文件的后缀
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        byte[] content = file.getBytes();
        return  aliyunOSSUtils.upload(content, extName);

    }

    @Transactional(rollbackFor = Exception.class)
    public R insertCheckGroupsWithSetMealId(List<Integer> ids, AddSetMealDto dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setMealMapper.insertSetMeal(setmeal);
        if (!ids.isEmpty()){
            Integer setmealId = setmeal.getId();
            setMealMapper.insertCheckGroupsWithSetMealId(setmealId,ids);
        }
        R r = new R();
        r.setFlag(true);
        return r;
    }

    public Setmeal getSetMealById(Integer id) {
       return setMealMapper.getSetMealById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    public R updateSetMealById(List<Integer> checkgroupIds, UpdateSetMealDto dto) {
        Integer setmealId = dto.getId();
        setMealMapper.deleteCheckGroupIdsBySetMealId(setmealId);
        setMealMapper.updateSetMealById(dto);
        if (!checkgroupIds.isEmpty()){
            setMealMapper.insertCheckGroupsWithSetMealId(setmealId,checkgroupIds);
        }

        R r = new R();
        r.setFlag(true);
        r.setMessage("修改完成");
        return r;
    }

    public R deleteSetMealById(Integer id) {
        R r = new R();
        if (CheckGroupMapper.getCheckGroupIdsBySetMealId(id).isEmpty()){
            setMealMapper.deleteSetMealById(id);
            r.setFlag(true);
            r.setMessage("删除成功");
            return r;
        }
        else {
            r.setFlag(false);
            r.setMessage("删除失败");
            return r;
        }

    }
}
